<template>
    <Modal v-model="visible" :title="title" width="600" :key="key">
        <Spin v-if="LoadingModal" size="large" fix></Spin>
        <Form ref="formData" :model="formData" :label-width="150" :rules="rules" :disabled="disableSubmit">
            <FormItem label="发放年月:" prop="ssfc0002">
                <DatePicker type="month" v-model="formData.ssfc0002" class="itemStyle" :options="options01"
                            :editable="false"></DatePicker>
            </FormItem>
            <FormItem label="救助对象类别:" prop="ssfc0004">
                <!--                    <dictSelect dictCode="systemsubtype" :bindData.sync="formData.ssfc0004"-->
                <!--                                :sel_val="formData.ssfc0004" style="width: 200px"/>-->
                <Select v-model="formData.ssfc0004" class="itemStyle" clearable>
                    <Option value="1010">城市最低生活保障对象</Option>
                    <Option value="1020">城市低保边缘家庭</Option>
                    <Option value="1030">农村最低生活保障对象</Option>
                    <Option value="1040">农村低保边缘家庭</Option>
                    <Option value="1050">城市单人保障对象</Option>
                    <Option value="1060">城市因病致贫保障对象</Option>
                    <Option value="1070">农村单人保障对象</Option>
                    <Option value="1080">农村因病致贫保障对象</Option>
                    <Option value="1090">农村建档立卡保障对象</Option>
                </Select>
            </FormItem>
            <FormItem label="备注:">
                <Input v-model="formData.remark" maxlength="100" show-word-limit type="textarea" placeholder=""
                       :rows="3"
                       class="itemStyle"/>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="text" size="large" @click="visible = false">取消</Button>
            <template v-if="!disableSubmit">
                <Button type="primary" size="large" @click="handleSubmit('formData')">确定</Button>
            </template>
        </div>
    </Modal>
</template>

<script>
  import { CommonMixin } from '@/views/common/js/CommonMixin'
  import DateFormat from '@/plugins/format.js'
  import { postAction } from '@/api/request'
  import dictSelect from '@/components/dictSelect/dictSelect.vue'

  export default {
    mixins: [CommonMixin],
    name: 'roster-model',
    data () {
      return {
        visible: false,
        LoadingModal: false,
        title: '花名册维护',
        key: '',
        Loading: false,
        queryParam: {},
        formData: {},
        fieldsToValidate: ['ssfc0002', 'ssfc0004'],
        url: {
          add: '/api/ac/subsistence/ss18900Service/createData',
          update: '/api/ac/subsistence/ss18900Service/updateData'
        },
        options01: {
          disabledDate (date) {
            // return date && date.valueOf() < Date.now();
            let nowTime = new Date()
            let month = nowTime.getMonth() + 1
            let year = nowTime.getFullYear()
            if (month - 1 == 0) {
              year = year - 1
              month = 12
            }
            return date && date.valueOf() < new Date(`${year}-${month}`)
          }
        },
        rules: {
          ssfc0002: [
            { required: true, message: '请选择"发放年月"', trigger: 'blur,change', type: 'date' }
          ],
          ssfc0004: [
            { required: true, message: '请选择"救助对象类别"', trigger: 'blur' }
          ]
        },
        disableSubmit: false,

      }
    },
    methods: {
      edit (row) {
        let that = this
        that.$refs['formData'].resetFields()
        that.visible = true
        that.formData = Object.assign({}, row)
        if (!that.formData.ssfc0002) {
          that.formData.ssfc0002 = new Date()
        } else {
          that.formData.ssfc0002 = DateFormat(that.formData.ssfc0002, 'YYYY-MM')
        }
      },
      cancel () {
        this.visible = false
      },
      handleSubmit (name) {
        let that = this
        let validate = true
        let err_msg = []
        that.LoadingModal = true
        for (let item of that.fieldsToValidate) {
          that.$refs[name].validateField(item, (error) => {
            if (error) {
              validate = false
              err_msg.push(error)
            }
          })
        }
        if (validate) {
          let dataParam = {
            id: that.formData.id,
            ssfc0002: DateFormat(that.formData.ssfc0002, 'YYYY-MM-DD'),
            ssfc0004: that.formData.ssfc0004,
            remark: that.formData.remark
          }
          console.log(dataParam)
          that.Loading = true
          let url = that.url.add
          if (this.formData.id) {
            url = that.url.update
          }
          postAction(url, dataParam).then((res) => {
            if (res.data.data.success == 0) {
              this.$Message.error(res.data.data.Message)
            } else {
              this.visible = false
              this.$Message.success(res.data.data.Message)
              this.$parent.loadData(1)
            }
          }).finally(() => {
            that.Loading = false
            that.LoadingModal = false
          })
        } else {
          that.$Message.error(err_msg[0])
          that.LoadingModal = false
          return
        }
      },
    }
  }
</script>

<style scoped>
    .itemStyle {
        width: 300px
    }
</style>
